package cn.credit.loan.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.credit.loan.entity.Approve;
import cn.credit.loan.service.approve.ApproveService;

@Controller
public class ApproveController {
	// 日志输出
	private Logger logger = Logger.getLogger(ApproveController.class);
	@Autowired
	private ApproveService approveService;
	
	
	/**
	 * 后台————业务经理登录操作的审批信息页面
	 * @return
	 */
	@RequestMapping(value="/approaction")
	public ModelAndView aprovehtml(){
		ModelAndView andView=new ModelAndView();
		andView.setViewName("after/approvelist");
		return andView;
	}

	
	/**
	 * 审核信息添加
	 * 
	 * @param approve
	 * @param model
	 * @return
	 */
	@RequestMapping("/approveadd")
	public String getApproveAdd(Approve approve, ModelMap model) {
		logger.info("##");
		try {
			approve.setCheckId(1);
			approve.setManagerId(1);
			approve.setCheckDate(new Date());
			approve.setManagerDate(new Date());
			approve.setCheckType("未审核");
			approve.setManagerType("未审核");
			boolean approves = approveService.getApproveAdd(approve);
			if (approves) {
				model.addAttribute("approve", approve);
				return "ApproveAdd";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 审核信息更新
	 * 
	 * @param approve
	 * @param model
	 * @return
	 */
	@RequestMapping("/approveupdate")
	@ResponseBody
	public String getApproveUpdate(Approve approve) {
		Map<String, Object> model=new HashMap<String,Object>();
		try {
			approve.setManagerDate(new Date());
			model.put("roveup", approveService.getApproveUpdate(approve));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONArray.toJSONString(model);
	}

	/**
	 * 审核信息删除
	 * @param approveId
	 * @param model
	 * @return
	 */
	@RequestMapping("/approvedelete")
	public String getApproveDelete(int approveId, ModelMap model) {
		try {
			boolean approveIds = approveService.getApproveDelete(approveId);
			if (approveIds) {
				model.addAttribute("approveId", approveId);
				return "ApproveDelete";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 后台————审核信息列表
	 * @param approve
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/approvelist")
	@ResponseBody
	public Object getApproveList(Approve approve,
			@RequestParam(value="pageNum",defaultValue="1",required=false)Integer pageNum,
			ModelMap model) {
		Map<String, Object> mapType=new HashMap<String,Object>();
		try {
			PageHelper.startPage(pageNum, 10);
			List<Approve> approvelist = approveService.getApproveList(approve);
			PageInfo<Approve> pageInfo=new PageInfo<Approve>(approvelist);
			mapType.put("aprove", approvelist);
			mapType.put("pageinfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONArray.toJSONString(mapType);
	}
	
}
